package com.example.androidproject.chat;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.androidproject.R;
import com.example.androidproject.chat.pojo.ChatGroupInfo;
import com.example.androidproject.chat.pojo.ChatMessage;
import com.example.androidproject.sqlite.MessageRepository;

import java.util.List;

public class ChatMessageAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private static final int TYPE_MY = 0;
    private static final int TYPE_OTHER = 1;
    private List<ChatMessage> chatMessages;
    private MessageRepository messageRepository;
    private boolean isDelete = false;

    public void setDelete(boolean delete) {
        isDelete = delete;
        notifyDataSetChanged();
    }

    public ChatMessageAdapter(List<ChatMessage> chatMessages, MessageRepository messageRepository) {
        this.chatMessages = chatMessages;
        this.messageRepository = messageRepository;
    }

    @Override
    public int getItemViewType(int position) {
        ChatMessage chatMessage = chatMessages.get(position);
        return chatMessage.getIdentity();
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        if (viewType == TYPE_MY) {
            View view = inflater.inflate(R.layout.fragment_chat_sent_message, parent, false);
            return new SentMessageViewHolder(view);
        } else {
            View view = inflater.inflate(R.layout.fragment_item_received_message, parent, false);
            return new ReceivedMessageViewHolder(view);
        }
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        ChatMessage message = chatMessages.get(position);
        if (holder instanceof SentMessageViewHolder) {
            ((SentMessageViewHolder) holder).bind(message, position);
        } else if (holder instanceof ReceivedMessageViewHolder) {
            ((ReceivedMessageViewHolder) holder).bind(message, position);
        }
    }

    @Override
    public int getItemCount() {
        return chatMessages.size();
    }

    class SentMessageViewHolder extends RecyclerView.ViewHolder {
        TextView textMessage;
        ImageView imageView;

        SentMessageViewHolder(@NonNull View itemView) {
            super(itemView);
            textMessage = itemView.findViewById(R.id.textMessage);
            imageView = itemView.findViewById(R.id.delete_button);
        }

        void bind(ChatMessage message, int position) {
            textMessage.setText(message.getMessage());
            if (!isDelete) {
                imageView.setVisibility(View.GONE);
            } else {
                imageView.setVisibility(View.VISIBLE);
            }
            imageView.setOnClickListener(v -> {
                chatMessages.remove(message);
                notifyDataSetChanged();
                messageRepository.deleteMessageById(position + 1);
            });
        }
    }

    class ReceivedMessageViewHolder extends RecyclerView.ViewHolder {
        ImageView imageProfile;
        TextView textMessage;
        ImageView imageView;

        ReceivedMessageViewHolder(@NonNull View itemView) {
            super(itemView);
            imageProfile = itemView.findViewById(R.id.imageProfile);
            textMessage = itemView.findViewById(R.id.textMessage);
            imageView = itemView.findViewById(R.id.delete_button);
        }

        void bind(ChatMessage message, int position) {
            textMessage.setText(message.getMessage());
            if (!isDelete) {
                imageView.setVisibility(View.GONE);
            } else {
                imageView.setVisibility(View.VISIBLE);
            }
            imageView.setOnClickListener(v -> {
                chatMessages.remove(message);
                notifyDataSetChanged();
                messageRepository.deleteMessageById(position + 1);
            });
        }
    }
}
